home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / T U R B O Language / Turbo Pascal V7.0 / DOCDEMO.ZIP / WORKERS.PAS < prev    next >
Pascal/Delphi Source File  |  1992-10-30  |  3KB  |  154 lines

  1. {************************************************}
  2. {                                                }
  3. {   Demo unit                                    }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit Workers;
  9.  
  10. interface
  11.  
  12. const
  13.   PayPeriods        =  26;       { per annum }
  14.   OvertimeThreshold =  80;       { per pay period }
  15.   OvertimeFactor    = 1.5;       { times normal hourly rate }
  16.  
  17. type
  18.   PEmployee = ^TEmployee;
  19.   TEmployee = object
  20.     Name: string[25];
  21.     Title: string[25];
  22.     Rate: Real;
  23.     constructor Init(AName, ATitle: String; ARate: Real);
  24.     destructor Done; virtual;
  25.     function GetName: String;
  26.     function GetPayAmount: Real; virtual;
  27.     function GetRate: Real;
  28.     function GetTitle: String;
  29.     procedure Show; virtual;
  30.   end;
  31.  
  32.   PHourly = ^THourly;
  33.   THourly = object(TEmployee)
  34.     Time: Integer;
  35.     constructor Init(AName, ATitle: String; ARate: Real; ATime: Integer);
  36.     function GetPayAmount: Real; virtual;
  37.     function GetTime: Integer;
  38.   end;
  39.  
  40.   PSalaried = ^TSalaried;
  41.   TSalaried = object(TEmployee)
  42.     function GetPayAmount: Real; virtual;
  43.   end;
  44.  
  45.   PCommissioned = ^TCommissioned;
  46.   TCommissioned = object(TSalaried)
  47.     Commission: Real;
  48.     SalesAmount: Real;
  49.     constructor Init(AName, ATitle: String;
  50.       ARate, ACommission, ASalesAmount: Real);
  51.     function GetPayAmount: Real; virtual;
  52.   end;
  53.  
  54.  
  55. implementation
  56.  
  57. function RoundPay(Wages: Real): Real;
  58. { Round pay amount to ignore any pay less than 1 penny }
  59. begin
  60.   RoundPay := Trunc(Wages * 100) / 100;
  61. end;
  62.  
  63.  
  64. { TEmployee }
  65. constructor TEmployee.Init(AName, ATitle: String; ARate: Real);
  66. begin
  67.   Name := AName;
  68.   Title := ATitle;
  69.   Rate := ARate;
  70. end;
  71.  
  72. destructor TEmployee.Done;
  73. begin
  74. end;
  75.  
  76. function TEmployee.GetPayAmount: Real;
  77. begin
  78.   RunError(211);         { Give runtime error }
  79. end;
  80.  
  81. function TEmployee.GetName: String;
  82. begin
  83.   GetName := Name;
  84. end;
  85.  
  86. function TEmployee.GetRate: Real;
  87. begin
  88.   GetRate := Rate;
  89. end;
  90.  
  91. function TEmployee.GetTitle: String;
  92. begin
  93.   GetTitle := Title;
  94. end;
  95.  
  96. procedure TEmployee.Show;
  97. var
  98.   S: string[25];
  99. begin
  100.   S := GetName;
  101.   Write(S, ' ':30 - Length(S));
  102.   S := GetTitle;
  103.   Write(S, ' ':30 - Length(S));
  104.   Writeln(GetPayAmount:10:2);
  105. end;
  106.  
  107. { THourly }
  108. constructor THourly.Init(AName, ATitle: String; ARate: Real; ATime: Integer);
  109. begin
  110.   TEmployee.Init(AName, ATitle, ARate);
  111.   Time := ATime;
  112. end;
  113.  
  114. function THourly.GetPayAmount: Real;
  115. var
  116.   OverTime: Integer;
  117. begin
  118.   Overtime := Time - OvertimeThreshold;
  119.   if Overtime > 0 then
  120.     GetPayAmount := RoundPay(OvertimeThreshold * Rate +
  121.       OverTime * OvertimeFactor * Rate)
  122.   else
  123.     GetPayAmount := RoundPay(Time * Rate);
  124. end;
  125.  
  126. function THourly.GetTime: Integer;
  127. begin
  128.   GetTime := Time;
  129. end;
  130.  
  131.  
  132. { TSalaried }
  133. function TSalaried.GetPayAmount: Real;
  134. begin
  135.   GetPayAmount := RoundPay(Rate / PayPeriods);
  136. end;
  137.  
  138.  
  139. { TCommissioned }
  140. constructor TCommissioned.Init(AName, ATitle: String;
  141.   ARate, ACommission, ASalesAmount: Real);
  142. begin
  143.   TSalaried.Init(AName, ATitle, ARate);
  144.   Commission := ACommission;
  145.   SalesAmount := ASalesAmount;
  146. end;
  147.  
  148. function TCommissioned.GetPayAmount: Real;
  149. begin
  150.   GetPayAmount := RoundPay(TSalaried.GetPayAmount + Commission * SalesAmount);
  151. end;
  152.  
  153. end.
  154.